<?php

class Admin_UserController extends Zend_Controller_Action
{

    public function init()
    {
		$userMapper = new Admin_Model_Mapper_User();		
		$this->view->activeUser = true;		
		$this->view->addScriptPath( APPLICATION_PATH . "/modules/user/views/scripts/");
    }

    public function indexAction()
    {
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);			
        $userMapper = new Admin_Model_Mapper_User();
		
		$this->view->paginator = $userMapper->getPaginator($this->getRequest()->getParam('page', 1), $this->getRequest()->getParam("limit", (int) $config->paginator->itemCountPerPage));	
		$this->view->resultSet = $userMapper->getPaginatorData($this->getRequest()->getParam('page', 1), $this->getRequest()->getParam("limit", (int) $config->paginator->itemCountPerPage));	 	
	}

    public function addAction()
    {
		$activityMapper = new Admin_Model_Mapper_Activity();
		$activity = new Admin_Model_Activity();
		$auth = Zend_Auth::getInstance();
		$userLanguageMapper = new Admin_Model_Mapper_UserLanguage();
		
		$form = new Admin_Form_User();		
		$form->submit->setLabel("Save");
		$this->view->form = $form;
		$userMapper = new Admin_Model_Mapper_User();
		$user = new Admin_Model_User();

		$languageMapper = new Admin_Model_Mapper_Language();
		$this->view->languageIsShowInTranslatorList =  $languageMapper->getTranslatorlistActive();	
		
		if(!$this->getRequest()->isPost()){
			return;			
		}			
		
		$formData = $this->getRequest()->getPost();		
		if(!$form->isValid($formData)){			
			$form->populate($formData);
			return;
		}				
		
		if(0 <> count($userMapper->getUserByUserLoginName(0, $form->getValue("UserLoginName")))){
			$this->view->insertErrorMessage = "can not insert duplicate user login name";
			$form->populate($formData);
			return;
		}
		
		if(0 <> count($userMapper->getUserByUserEmail($form->getValue("UserEmail"), 0))){
			$this->view->insertErrorMessage = "can not insert duplicate email address";
			$form->populate($formData);
			return;
		}
				
		$user->UserName = $form->getValue("UserName");			
		$user->UserEmail = $form->getValue("UserEmail");			
		$user->UserLoginName = $form->getValue("UserLoginName");			
		$user->UserPassword = $form->getValue("UserPassword");		
		$user->UserRole = $form->getValue("UserRole");		
		$user->UserIsActive = $form->getValue("UserIsActive") == "Y" ? "Y" : "N";		
				
		$userId = $userMapper->save($user);
		foreach($form->getValue("SourcelanguageID") as $languageID){
			if($this->_getParam("hiddenSourceTargetLanguage_$languageID", "0") !="0"){
				$targetLanguage = explode(",", $this->_getParam("hiddenSourceTargetLanguage_$languageID", 0));
			
				for($i=0; $i< count($targetLanguage); $i++){
					$userLanguageMapper->save(null, $userId, $languageID, $targetLanguage[$i]);	
				}
			}				
		}
		
		/*$activity->setUserName($auth->getIdentity()->UserLoginName)
				  ->setUserActivity("User add UserID " .  $userID)
				  ->setUserActivityDateTime(date("Y-m-d : H:i:s", time()));
		$activityMapper->save($activity);	
		*/
		
		$this->_helper->redirector("index");
    }

    public function editAction()
    {
		$activityMapper = new Admin_Model_Mapper_Activity();
		$activity = new Admin_Model_Activity();
		$auth = Zend_Auth::getInstance();
	
		$form = new Admin_Form_User();			
		$form->submit->setLabel("Save");
		$this->view->form = $form;
		$userMapper = new Admin_Model_Mapper_User();					
		$user = new Admin_Model_User();
		$userLanguageMapper = new Admin_Model_Mapper_UserLanguage();
		
		$languageMapper = new Admin_Model_Mapper_Language();
		$this->view->languageIsShowInTranslatorList =  $languageMapper->getTranslatorlistActive();
				
		if(!$this->getRequest()->isPost()){
			if($this->_getParam("userid", 0) <= 0){
				return ;
			}	
			
			$userMapper->find($this->_getParam("userid", 0), $user);
			$formData = array("UserID" => $user->getUserId(), "UserName" => $user->getUserName() ,"UserEmail" => $user->getUserEmail(), 
							"UserLoginName" => $user->getUserLoginName(),"UserPassword" => $user->getUserPassword(), "UserRole" => $user->getUserRole(),
							"UserIsActive" => $user->getUserIsActive());
						
			if($user->UserRole == Admin_Model_DbTable_User::USER_TRANSLATOR){				
				$formData["SourcelanguageID"] = $userLanguageMapper->getSourceLanguages($user->getUserId());
			}
					
			$form->populate($formData);
			$this->createTargetLanguageWhenEdit($form, $user->getUserId());				
			return;
		}				
				
		if($this->getRequest()->getPost("UserPassword") == ""){
			$form->UserPassword->setRequired(false);	
			$form->UserConfirmPassword->setRequired(false);				
		}
	
		$formData = $this->getRequest()->getPost();			
		if(!$form->isValid($formData)){
			return $form->populate($formData);	
		}
		
		if(0 <> count($userMapper->getUserByUserLoginName($form->getValue("UserID"), $form->getValue("UserLoginName")))){
			$this->view->updateErrorMessage = "can not update duplicate user login name";
			return $form->populate($formData);
		}
		
		if(0 <> count($userMapper->getUserByUserEmail($form->getValue("UserEmail"), $form->getValue("UserID")))){
			$this->view->updateErrorMessage = "can not update duplicate email address";
			$form->populate($formData);
			return;
		}
		
		$user->UserID = (int)$form->getValue("UserID");	
		$user->UserName = $form->getValue("UserName");			
		$user->UserEmail = $form->getValue("UserEmail");			
		$user->UserLoginName = $form->getValue("UserLoginName");			
		$user->UserPassword = $form->getValue("UserPassword");				
		$user->UserRole = $form->getValue("UserRole");		
		$user->UserIsActive = $form->getValue("UserIsActive") == "Y" ? "Y" : "N";			
		$userID = $userMapper->save($user);
		
		if($form->getValue("UserRole") == Admin_Model_DbTable_User::USER_TRANSLATOR){
			$userLanguageMapper->delete($user->getUserId());
			foreach($form->getValue("SourcelanguageID") as $languageID){
				if($this->_getParam("hiddenSourceTargetLanguage_$languageID", "0") !="0"){
					$targetLanguage = explode(",", $this->_getParam("hiddenSourceTargetLanguage_$languageID", 0));
										
					for($i=0; $i< count($targetLanguage); $i++){						
						$result = $userLanguageMapper->getUserLanguageByUserIDAndLanguageID($userID, $languageID, $targetLanguage[$i]);
						$userLanguageMapper->save( count($result) ==0 ? null : $result[0]->UserLanguageID, $user->UserID, $languageID, $targetLanguage[$i]);	
					}
				}				
			}
		}
		
		/*
		$activity->setUserName($auth->getIdentity()->UserLoginName)
				  ->setUserActivity("User Edit UserID " .  $userID)
				  ->setUserActivityDateTime(date("Y-m-d : H:i:s", time()));
		$activityMapper->save($activity);	
		*/
		$this->_helper->redirector("index");		
    }

    public function deleteAction()
    {	
		$activityMapper = new Admin_Model_Mapper_Activity();
		$activity = new Admin_Model_Activity();
		$auth = Zend_Auth::getInstance();
		
		$userMapper = new Admin_Model_Mapper_User();	
		$user = new Admin_Model_User();
				
		if(!$this->getRequest()->isPost()){
			$userId = $this->_getParam("userid", 0);
			$userMapper->find($userId, $user);
			$this->view->user = $user;				
			return;
		}

		if($this->getRequest()->getPost("del") == "Yes"){
			$userId = $this->getRequest()->getPost("userid");						
			$userMapper->deleteUser($userId);
			
			/*
			$activity->setUserName($auth->getIdentity()->UserLoginName)
				  ->setUserActivity("User Delete UserID " .  $userID)
				  ->setUserActivityDateTime(date("Y-m-d : H:i:s", time()));
			$activityMapper->save($activity);	
			*/
		}		
		
		$this->_helper->redirector("index");
    }

    public function viewdetailAction()
    {
        $userId = $this->_getParam("userid", 0);	
		
		if($userId > 0){
			$userMapper = new Admin_Model_Mapper_User();	
			$user = new Admin_Model_User();
			$userMapper->find($userId, $user);			
			$this->view->user = $user;	

			if($user->UserRole == Admin_Model_DbTable_User::USER_TRANSLATOR){
				$userLanguageMapper = new Admin_Model_Mapper_UserLanguage();
				$targetlanguageID = $userLanguageMapper->getTargetLanguageNames($userId);
				$this->view->targetlanguageID = $targetlanguageID;	
			}	
		}		
    }

	private function createTargetLanguageWhenEdit($form, $userID){
		$userLanguageMapper = new Admin_Model_Mapper_UserLanguage;
		
		$languages = $form->getValue("SourcelanguageID");
		if(!empty($languages)){
			foreach($languages as $sourceLanguageID){
				$targetLanguages = $userLanguageMapper->getTargetLanguages($userID, $sourceLanguageID);	
				$targetArray = Array();
				foreach($targetLanguages as $language){							
					$targetArray[] = $language->LanguageID;
				}	
			
				$form->addElement("hidden" , "hiddenSourceTargetLanguage_".$sourceLanguageID, array("value" => implode(",", $targetArray)));
				$form->getElement("hiddenSourceTargetLanguage_".$sourceLanguageID)
					 ->setDecorators(array('ViewHelper'));											
			}
		}
	}	

}









